package com.github.wxiaoqi.security.auth.feignserver;

import java.util.HashMap;
import java.util.Map;

import com.github.wxiaoqi.security.auth.service.AuthService;
import com.github.wxiaoqi.security.auth.util.user.JwtAuthenticationRequest;
import com.github.wxiaoqi.security.common.util.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.wxiaoqi.security.common.msg.ResponseCode;

/**
 * @author bolei
 * @create 2018-4-2 20:30
 * 业务系统登陆授权接口，通过feign调用
 */
@Controller
@RequestMapping("feign/auth")
public class FeignUserAuth {
	@Autowired
	private AuthService authService;
	
	@RequestMapping(value = "/getUserToken",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getUserToken(@RequestBody Map<String, Object> param) {
		Map<String, Object> result = new HashMap<String, Object>();
		String platformId = (String) param.get("platformId");
		String username = (String) param.get("username");
		String password = (String) param.get("password");
		JwtAuthenticationRequest authenticationRequest = new JwtAuthenticationRequest();
		authenticationRequest.setPlatformId(platformId);
		authenticationRequest.setUsername(username);
		authenticationRequest.setPassword(password);
		try {
			Map<String, Object> respMap = authService.login(authenticationRequest);
			return ResponseHelper.success(respMap, null, ResponseCode.OK.getCode(), ResponseCode.OK.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseHelper.failure(ResponseCode.CUST_NOTEXIST.getCode(), ResponseCode.CUST_NOTEXIST.getMessage());
		}
    }

}
